//
//  CreateViewController.m
//  FVQuickMessage
//
//  Created by dllo on 16/8/22.
//  Copyright © 2016年 zty. All rights reserved.
//

#import "CreateViewController.h"

@interface CreateViewController ()

@property (nonatomic, retain) UIImageView *imageView;

@end

@implementation CreateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor orangeColor];
    
    self.navigationController.navigationBar.translucent = NO;
    
    self.navigationItem.title = @"我的二维码";
    
    [self logoQrCode];
    
    // Do any additional setup after loading the view.
}

-(void)logoQrCode {
    
    
    
    // 二维码过滤镜
    CIFilter *qrImageFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    
    // 设置过滤镜默认属性
    [qrImageFilter setDefaults];
    
    // 将字符串转换成NSData
    NSString *username = [[EaseMob sharedInstance].chatManager loginInfo][@"username"];
    
    NSData *qrImageData = [username dataUsingEncoding:NSUTF8StringEncoding];
    
    // 设置过滤器的输入值，KVC赋值
    [qrImageFilter setValue:qrImageData forKey:@"inputMessage"];
    
    // 取出图片
    CIImage *qrImage = [qrImageFilter outputImage];
    qrImage = [qrImage imageByApplyingTransform:CGAffineTransformMakeScale(20, 20)];
    
    // 转换成UI类型
    UIImage *qrUIImage = [UIImage imageWithCIImage:qrImage];
    
    
    // 开启绘图，获取图形上下文
    UIGraphicsBeginImageContext(qrUIImage.size);
    
    // 把二维码图片画上去
    [qrUIImage drawInRect:CGRectMake(0, 0, qrUIImage.size.width, qrUIImage.size.height)];
    
    // 把小图片画上去
    UIImage *sImage = [UIImage imageNamed:@""];
    CGFloat sImageW = 100;
    CGFloat sImageH = sImageW;
    CGFloat sImageX = (qrUIImage.size.width - sImageW) * 0.5;
    CGFloat sImageY = (qrUIImage.size.height - sImageH) * 0.5;
    
    [sImage drawInRect:CGRectMake(sImageX, sImageY, sImageW, sImageH)];
    
    // 获取当前这张图片
    UIImage *finalyImage = UIGraphicsGetImageFromCurrentImageContext();
    
    // 关闭图形上下文
    UIGraphicsEndImageContext();
    
    self.imageView.image = finalyImage;
    
}

-(UIImageView *)imageView {
    
    if (_imageView == nil) {
        _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 50, WIDTH - 100, WIDTH - 100)];
        
        [self.view addSubview:_imageView];
    }
    return _imageView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
